import React from "react";
import { useState } from "react";

import {useHistory} from "react-router-dom";
// import Home from "../home/home.js";
import service from "../services.js";

function Login() {
  const [user, getUser] = useState("");
  const [pass, getPass] = useState("");
  const history =useHistory()
  // console.log(history);
  let loginAdd = async () => {    
    const res = await service.LoginAdd(user, pass);
    console.log(res);
    if (res.codo === 0) {
      localStorage.setItem("id", res.userInfo[0]._id);
      // history.push({pathname:'/home'.})   
      history.push("/home")   
    }

    if (!res.codo) return false;
  };
  return (
    <>
  
      用户名：
      <input
        type="text"
        onChange={(e) => {
          getUser(e.target.value);
        }}
        />
      密码：
      <input
        type="text"
        onChange={(e) => {
          getPass(e.target.value);
        }}
        />
        {/* <Router> */}
          {/* <Route> */}
      <button onClick={loginAdd}>登录</button>
      {/* </Route> */}
      {/* </Router> */}
      {/* <Switch>
        <Route path='/login' component={Login}></Route>
      </Switch> */}
    </>
  );
}
// export default withRouter(Login);
export default Login;
